<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../js/babel.min.js"></script>
    <script src="../../js/react.development.js"></script>
    <script src="../../js/react-dom.development.js"></script>
    <script src="../../js/prop-types.js"></script>
</head>

<body>
    <div id="example1"></div>
    <div id="example2"></div>
</body>

</html>
<script type="text/babel">
class App extends React.Component{
   constructor(props){
       super(props)
       this.state={
           todoList:["1","2"]
       }
       this.addTODO=this.addTODO.bind(this)
   }
   addTODO(todo){
       const {todoList}=this.state;
       todoList.unshift(todo)
       //更新状态
        this.setState({todoList})
   }
    render(){
        const todos=this.state.todoList;
        return <div>
            <h1>TODO List</h1>
            <Add count={todos.length} addTODO={this.addTODO}/>
            <List todoList={todos}/>
        </div>
    }
}
class Add extends React.Component{
    constructor(props){
       super(props)
       this.add=this.add.bind(this);
   }
   add(){
       const todo=this.input.value.trim();
       if (!todo) return
      this.props.addTODO(todo)
      this.input.value=''
   }
    render(){
        return (
            <div>
                <input type="text" ref={input=>this.input=input}/>
                <button onClick={this.add}>add #{this.props.count+1}</button>
            </div>
        )
    }
}
Add.propTypes={
    count:PropTypes.number.isRequired,
    addTODO:PropTypes.func.isRequired
}
class List extends React.Component{
    render(){
        return(
            <div>
               <ul>
                 {
                     this.props.todoList.map((value,index)=>{
                        return <li key={index}>{value}</li>
                     })
                 }
               </ul>
            </div>
        ) 
    }
}
List.propTypes={
    todoList:PropTypes.array.isRequired
}
ReactDOM.render(<App/>,document.getElementById('example1'))

</script>